<template>
    <div class="wedding-editor" ref="editor">
        <!-- 头部 -->
        <header class="editor-header">
            <span></span>
            <span></span>
            <span></span>
        </header>
        <!-- 日期 -->
        <p class="code">
            Last login: <span>{{ startDate }}</span>
        </p>
        <!--代码编辑区-->
        <pre><code v-html="highlightCode"></code>
        </pre>
        <!-- 邀请函开关 -->
        <div
            class="editor-open"
            v-if="(canStart || hasClosed) && !canOpen"
            @click="canOpen = true"
        >
            <svg
                viewBox="0 0 24 24"
                width="18"
                height="18"
                stroke="currentColor"
                stroke-width="2"
                fill="none"
                stroke-linecap="round"
                stroke-linejoin="round"
                class="css-i6dzq1"
            >
                <rect x="3" y="3" width="18" height="18" rx="2" ry="2"></rect>
                <line x1="9" y1="3" x2="9" y2="21"></line>
            </svg>
        </div>
        <Execution
            :canExecute="canExecute"
            @onUpdating="scrollToBottom"
            @onFinish="canOpen = true"
        />
        <Invitation
            :canOpen="canOpen"
            @onClose="handleClose"
            @sendBarrage="onAfterSending"
        />
        <Barrage :wish="wish" :canStart="canStart" />
    </div>
</template>

<script setup>
import prism from "prismjs"; // 代码高亮显示
import { ref, computed, onMounted } from "vue";
import Execution from "./Execution";
import Invitation from "./Invitation";
import Barrage from "./Barrage";
import { code } from "../mock/data";
// 登录日期
const startDate = ref(new Date().toDateString());
// 光标显隐
const isCurVisible = ref(1);
// 打印代码
const showCode = ref(code);
// 当前代码
const currentCode = ref("");
// 编辑元素
const editor = ref();
// 是否执行
const canExecute = ref(false);
// 是否打开
const canOpen = ref(false);
// 是否关闭
const hasClosed = ref(false);
// 是否开始
const canStart = ref(false);
// 祝福语句
const wish = ref("");
// 高亮代码
const highlightCode = computed(() => {
    const code = prism.highlight(currentCode.value, prism.languages.javascript); // 需要高亮代码
    return `${code}<span style="opacity:${isCurVisible.value}"> ▍</span>`;
});
// 代码输入
const progressivelyTyping = () => {
    return new Promise((resolve) => {
        let count = 0,
            typingCount = 0,
            typing;
        // 写代码每一帧的函数
        let step = () => {
            let randomNumber = Math.round(Math.random() * 6); // 随机数
            // 模拟打字的随机速度
            if (count % 2 === 0 && randomNumber % 4 === 0) {
                currentCode.value = showCode.value.substring(0, typingCount);
                typingCount++;
            }
            // 大约每 24 帧光标闪动一次
            if (count % 24 === 0) {
                isCurVisible.value = isCurVisible.value === 0 ? 1 : 0;
            }
            count++;
            if (typingCount <= showCode.value.length) {
                typing = requestAnimationFrame(step); // 启动定时器
            } else {
                resolve();
                canExecute.value = true; // 开始初始化执行打包动画
                cancelAnimationFrame(typing);
            }
        };
        typing = requestAnimationFrame(step);
    });
};
// 滚动底部
const scrollToBottom = () => {
    editor.value.scrollTop = document.querySelector("#app").offsetHeight;
};
onMounted(() => {
    progressivelyTyping();
});
// 关闭邀请函
const handleClose = () => {
    canOpen.value = false;
    hasClosed.value = true;
};
// 发送祝福语
const onAfterSending = (msg) => {
    wish.value = msg;
    canOpen.value = false;
    setTimeout(() => {
        canStart.value = true;
    }, 800);
};
</script>

<style lang="less">
.wedding-editor {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    padding: 15px;
    padding-top: 50px;
    overflow-x: hidden;
    overflow-y: auto;
    z-index: 1;
    transform-origin: 0 0;
    transition: all 1.6s cubic-bezier(0.4, 0, 1, 1);
    .editor-header {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        padding: 12px;
        overflow: hidden;
        background: #2b2b48;
        z-index: 3;
        > span {
            float: left;
            display: block;
            width: 16px;
            height: 16px;
            margin-right: 5px;
            border-radius: 8px;
            background: #fc615d;
            &:nth-child(2) {
                background: #fdbc40;
            }
            &:nth-child(3) {
                background: #34c84a;
            }
        }
    }
    p.code {
        margin: 0;
        color: #bbb;
        line-height: 1.2;
        font-family: "Roboto Mono", "Menlo", "Monaco", Courier, monospace !important;
        font-weight: 500 !important;
        font-size: 16px !important;
    }
    pre {
        margin: 0;
        white-space: pre-wrap;
        code {
            white-space: pre-wrap;
            word-break: break-all;
            font-size: 16px !important;
            margin: 0;
            color: #bbb;
            line-height: 1.2;
            font-family: "Roboto Mono", "Menlo", "Monaco", Courier, monospace !important;
            font-weight: 500 !important;
            background: transparent;
        }
    }
    .editor-open {
        position: fixed;
        right: 20px;
        bottom: 20px;
        width: 40px;
        height: 40px;
        padding: 6px;
        border-radius: 20px;
        text-align: center;
        line-height: 18px;
        border: 5px solid #ffd69b;
        color: #a9895d;
        background: #fff1de;
        z-index: 1000;
    }
}
</style>
